import { fetchUserCenter } from '../../services/usercenter/fetchUsercenter';
import Toast from 'tdesign-miniprogram/toast/index';
import { db, app } from '../../config/index'

const menuData = [
  [
    {
      title: '收货地址',
      tit: '',
      url: '',
      type: 'address',
    },
  ]
];

const getDefaultData = () => ({
  showMakePhone: false,
  userInfo: {},
  menuData,
  customerServiceInfo: {},
  currAuthStep: 1,
  versionNo: '',
});

Page({
  data: getDefaultData(),

  onLoad() {
    this.getVersionInfo();
    if(app.globalData.userInfo){
      this.setData({
        userInfo: app.globalData.userInfo,
        currAuthStep: 2,
      })
    }
  },

  onShow() {
    this.getTabBar().init();
    this.init();
    this.setData({
      userInfo: app.globalData.userInfo
    })
  },
  
  onPullDownRefresh() {
    this.init();
  },

  init() {
    this.fetUseriInfoHandle();
  },

  fetUseriInfoHandle() {
    fetchUserCenter().then(res => {
      this.setData({
        userInfo: res,
        currAuthStep: 2,
      });
      wx.cloud.database().collection('book-user').where({
        _openid: app.globalData.userInfo.openid
      }).get({
        success:(result)=>{
          if(result.data.length == 0){
            wx.cloud.database().collection('book-user').add({
              data:{
                avatarUrl: res.avatarUrl,
                nickName: res.nickName,
                _createTime: Date.now(),
                _updateTime: Date.now(),
              },
              success:(res)=>{
                Toast({
                  context: this,
                  selector: '#t-toast',
                  message: '登录成功',
                  icon: '',
                  duration: 1000,
                });
              }
            })
          }
        }
      })
      wx.stopPullDownRefresh();
    });
  },

  onClickCell({ currentTarget }) {
    const { type } = currentTarget.dataset;

    switch (type) {
      case 'address': {
        wx.navigateTo({ url: '/pages/usercenter/address/list/index' });
        break;
      }
      case 'service': {
        this.openMakePhone();
        break;
      }
      default: {
        Toast({
          context: this,
          selector: '#t-toast',
          message: '未知跳转',
          icon: '',
          duration: 1000,
        });
        break;
      }
    }
  },

  jumpNav(e) {
    const status = e.detail.tabType;

    if (status === 0) {
      wx.navigateTo({ url: '/pages/order/after-service-list/index' });
    } else {
      wx.navigateTo({ url: `/pages/order/order-list/index?status=${status}` });
    }
  },

  jumpAllOrder() {
    wx.navigateTo({ url: '/pages/order/order-list/index' });
  },

  openMakePhone() {
    this.setData({ showMakePhone: true });
  },

  closeMakePhone() {
    this.setData({ showMakePhone: false });
  },

  call() {
    wx.makePhoneCall({
      phoneNumber: this.data.customerServiceInfo.servicePhone,
    });
  },

  gotoUserEditPage() {
    const { currAuthStep } = this.data;
    if (currAuthStep === 2) {
      wx.navigateTo({ url: '/pages/usercenter/person-info/index' });
    } else {
      this.fetUseriInfoHandle();
    }
  },

  getVersionInfo() {
    const versionInfo = wx.getAccountInfoSync();
    const { version, envVersion = __wxConfig } = versionInfo.miniProgram;
    this.setData({
      versionNo: envVersion === 'release' ? version : envVersion,
    });
  },
});
